/*
 * @Description: 电子围栏
 * @Date: 2022-04-05 14:56:44
 * @Author: yeming
 */
import { transform, fromLonLat } from 'ol/proj.js'
import { Point } from '../model/point.js'
import { Circle } from '../model/circle.js'
import { Polygon } from '../model/polygon.js'
import { removeFeatureAndDeleteHashMap, removeAllFeatureAndDeleteHashMap } from '../common/removeFeature.js'

const AIRLINE_GIVE_POINT = './static/olcsMap/img/give_point_to_fly.png'

class ElectricFence {
  constructor(ol2d, circleHashMap, polygonHashMap, polygonPointHashMap, vectorSource) {
    this.ol2d = ol2d
    this.circleHashMap = circleHashMap
    this.polygonHashMap = polygonHashMap
    this.polygonPointHashMap = polygonPointHashMap
    this.vectorSource = vectorSource
  }

  /**
   * @desc: 添加圆形电子围栏（鼠标左键第一次点击为圆心，鼠标移动，圆形大小开始发生变化，鼠标左键再次点击结束当前绘制）
   * @param {Number} sessionId
   * @param {Array} latlng
   * @param {Number} radius
   * @param {Object} style
   */
  drawCircleElectricFence (sessionId, latlng, radius, style) {
    let hasElectricFence = this.circleHashMap.has(sessionId)
    if (!hasElectricFence) {
      this.circleElectricFenceSessionId = sessionId
      let circle = new Circle(this.ol2d, this.vectorSource)
      circle.addCircle2(sessionId, latlng, 6, 1, radius, style, true)
      this.circleHashMap.set(sessionId, { 'item2d': circle })
    } else {
      let currentElectricFence = this.circleHashMap.get(sessionId)
      let feature = currentElectricFence.item2d.feature
      let properties = feature.getProperties()
      properties.latlng = latlng
      properties.radius = radius
      feature.setProperties(properties)
      feature.getGeometry().setCenterAndRadius(fromLonLat(latlng), radius)
    }
  }

  /**
   * @desc: 是否显示圆形电子围栏
   * @param {Number} sessionId
   * @param {Number} isShow 1-显示 0-不显示
   */
  switchShowCircleElectricFence (sessionId, isShow) {
    let currentElectricFence = this.circleHashMap.get(sessionId)
    if (!currentElectricFence) return false

    let feature = currentElectricFence.item2d.feature
    let properties = feature.getProperties()
    if (isShow) feature.getGeometry().setRadius(0)
    else {
      let latlng = properties.latlng
      let radius = properties.radius
      feature.getGeometry().setCenterAndRadius(fromLonLat(latlng), radius)
    }
  }

  /**
   * @desc: 删除圆形电子围栏
   * @param {Number} sessionId
   */
  delCircleElectricFence (sessionId) {
    removeFeatureAndDeleteHashMap(sessionId, this.circleHashMap, this.vectorSource)
  }

  /**
   * @desc: 删除所有圆形电子围栏
   */
  delAllCircleElectricFence () {
    removeAllFeatureAndDeleteHashMap(this.circleHashMap, this.vectorSource)
  }

  /**
   * @desc: 绘制多边形电子围栏
   * @param {*} sessionId
   * @param {*} latlng
   * @return {*}
   * @use: 使用示例
   */
  drawPolygonElectricFence (sessionId, latlng) {
    let hasPolygonElectricFence = this.polygonHashMap.has(sessionId)
    if (!hasPolygonElectricFence) {
      let polygon = new Polygon(this.ol2d, this.vectorSource)
      polygon.drawPolygonElectricFence(sessionId, [latlng])
      this.polygonHashMap.set(sessionId, { 'item2d': polygon, 'points': [] })
    } else {
      let currentElectricFence = this.polygonHashMap.get(sessionId)
      let currentGeomPolygon = currentElectricFence.item2d.electricFencePolygon
      let currentCoordinates = currentGeomPolygon.getCoordinates()
      currentCoordinates[0].push(fromLonLat(latlng))
      currentGeomPolygon.setCoordinates(currentCoordinates)
    }

    let id = new Date().getTime()
    let point = new Point(this.ol2d, this.vectorSource)
    let currentPoints = this.polygonHashMap.get(sessionId).points
    point.addSinglePoint(id, latlng, 11, currentPoints.length, null, true, sessionId, true)
    currentPoints.push(point)
  }

  /**
   * @desc: 渲染多边形电子围栏
   * @param {Number} sessionId
   * @param {Number} latlng [[lon, lat, h], [lon, lat, h], ......, [lon, lat, h]]
   */
  renderPolygonElectricFence (sessionId, latlng) {
    if (latlng.length <= 2) return false
    let polygon = new Polygon(this.ol2d, this.vectorSource)
    polygon.drawPolygonElectricFence(sessionId, latlng)
    this.polygonHashMap.set(sessionId, { 'item2d': polygon, 'points': [] })

    let currentPoints = this.polygonHashMap.get(sessionId).points
    latlng.forEach((item, index) => {
      let id = new Date().getTime()
      let point = new Point(this.ol2d, this.vectorSource)
      point.addSinglePoint(id, item, 11, index, null, true, sessionId, true)
      currentPoints.push(point)
    })
  }

  /**
   * @desc: 更新多边形电子围栏
   * @param {Array} latlng
   * @param {Number} index
   * @param {Number} parentId
   */
  updatePolygonElectricFencePoint (latlng, index, parentId) {
    let currentGeomPolygon = this.polygonHashMap.get(parentId).item2d.electricFencePolygon
    let currentCoordinates = currentGeomPolygon.getCoordinates()
    currentCoordinates[0][index] = fromLonLat(latlng)
    currentGeomPolygon.setCoordinates(currentCoordinates)
  }

  /**
   * @desc: 删除多边形电子围栏
   * @param {Number} sessionId
   */
  delPolygonElectricFence (sessionId) {
    let currentPoints = this.polygonHashMap.get(sessionId).points
    currentPoints.forEach(item => {
      this.vectorSource.removeFeature(item.feature)
    })
    removeFeatureAndDeleteHashMap(sessionId, this.polygonHashMap, this.vectorSource)
  }
}

export default ElectricFence
